<?php

/*                                                                        *
 * This script belongs to the FLOW3 package "Fluid".                      *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation, either version 3 of the License, or (at your *
 * option) any later version.                                             *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 *
 * Enter description here ...
 * @author flo
 *
 */
class Tx_IfpCurriculum2_ViewHelpers_CalDataViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {





	/**
	 *
	 * Enter description here ...
	 * @param array $veranstaltungstage
	 * @param integer $year
	 * @param integer $month
	 */
	public function render(array $veranstaltungstage,$year,$month) {

		$days=array();
		foreach ($veranstaltungstage as $veranstaltungstag) {
			$days[$veranstaltungstag->getBeginn()->format("j")][]=$veranstaltungstag;
		}
		
		$first_of_month = mktime(0,0,0,$month,1,$year);


		list($month, $year, $month_name, $weekday) = explode(',',strftime('%m,%Y,%B,%w',$first_of_month));
		$weekday = ($weekday +6) % 7; #adjust for $first_day
		$Monat2txt = array('0','Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');

		$title   = htmlentities(ucfirst($Monat2txt[date("n",$first_of_month)])).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names

		#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
		$str="";
		if($weekday > 0){
			$str.="<tr>";
			for($i=$weekday;$i>0;$i--){
				$str.= '<td class="padding">&nbsp;</td>'; #initial 'empty' days
			}
		}
		for($day=1,$days_in_month=date('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
			if($weekday == 7){
				$str.="</tr><tr>";
				$weekday   = 0; #start a new week

			}
			if(isset($days[$day]) and is_array($days[$day])){
				
				$str .= '<td  class="date_has_event">'.$day;
				$str .=' <div class="events"> <ul> ';
				foreach ($days[$day] as $event) {
					$str.=' <li>
                  				<span class="title">';
					if(!$event->getVeranstaltung()){
						continue;
					}
					if($event->getVeranstaltung()->getKurs()){
						$link=$event->getVeranstaltung()->getKurs()->getStammtitelDetail();
					}else{
						$link=$event->getVeranstaltung()->getTitel();
					}
					$str.=	$this->makelink($link,"show",array("veranstaltung"=>$event->getVeranstaltung()),"Veranstaltung","C_Veranstaltung",80);
					$str.='</span>
                 			 <span class="desc">';
					if($event->getVeranstaltung()->getKurs()){
						//$str.='Dozent:'.$event->getVeranstaltung()->getKurs()->getDozent()->getName().' | Modul: '.$event->getVeranstaltung()->getKursstamm()->getModulname().' </span> ';
					}
					$str.='</li> ';
				}


				$str.='</ul> </div> ';
			}
			else if($day==date('j')&&$month==date("n")){
				$str .=  '<td class="today">'.$day;
			}else{
				$str .=  '<td>'.$day;
			}

			$str .= '</td>';

		}
		if($weekday != 7){
			for($i=$weekday;$i<7;$i++){
				$str.= '<td class="padding">&nbsp;</td>'; #remaining 'empty' days
			}
			//$str .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days
		}


		return $str;
	}
	
	protected function makelink($text,$action = NULL, array $arguments = array(), $controller = NULL, $pluginName = NULL, $pageUid = NULL) {
		$uriBuilder = $this->controllerContext->getUriBuilder();
		$uri = $uriBuilder
			->reset()
			->setTargetPageUid($pageUid)
			->setaddQueryString(true)
			->uriFor($action, $arguments, $controller, null, $pluginName);
			
		
		Return '<a href="'.$uri.'">'.$text.'</a>';
	}


}

?>